package orest.front.membercenter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ssta.common.Constants;
import net.ssta.common.DataBaseConfig;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.util.UUIDGenerator;
import net.ssta.core.web.RequestKit;
import net.ssta.util.bill.UserPoint;
import net.ssta.util.mail.MailConfig;
import net.ssta.util.mail.MailSender;
import net.ssta.util.mail.MailSenderInfo;

import orest.back.market.MarketSql;

import org.apache.log4j.Logger;

import com.rc.sdk.crypt.CryptFactory;


public class AchieveTrackHandler {
	private static Logger log = Logger.getLogger(AchieveTrackHandler.class.getClass()); 
	
	/**
	 * 根据人员id获取机构的科技成果信息
	 * @return
	 * @throws SQLException
	 */
	public RecordSet unitAchieveList(String userid,PagingInfo pagingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	    	String sql = AchieveTrackSql.getArchieveByUserid(userid);
	    	rs=dbe.find(sql,pagingInfo);
	    	
		} catch (SQLException e) {
			log.error("获取机构成果信息出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	/**
	 * 根据人员id、科技成果id获取科技成果跟踪信息列表
	 * @return
	 * @throws SQLException
	 */
	public RecordSet unitAchieveTrackList(String userid,String kjcgid) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	    	String sql = AchieveTrackSql.getArchieveTrackByUserid(userid,kjcgid);
	    	rs=dbe.find(sql);
	    	
		} catch (SQLException e) {
			log.error("获取机构成果信息出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	/**
	 * 统计各成果对应的跟踪信息数量，返回map形式，name为成果id，value为对应的成果跟踪数量
	 * **/
	public Map achieveTrackCount(String userid)
    {
		Map resultmap = new HashMap();
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	    	String sql = AchieveTrackSql.countArchieveByUserid(userid);
	    	
	    	rs=dbe.find(sql);
	    	if(rs!=null){
	    		for(int i=0;i<rs.size();i++){
	    			Record record = rs.get(i);
	    			resultmap.put(record.getString("KJCGID",""),record.getString("KJCGCOUNT",""));
	    		}
	    	}
	    	
		} catch (SQLException e) {
			log.error("获取机构成果信息出错："+e);
			e.printStackTrace();
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return resultmap;
    }
	
	/**
	 * 根据科技成果ID获取科技成果详细信息
	 * @return
	 * @throws SQLException
	 */
	public Record achieveInfo(String achieveid) throws SQLException {
		RecordSet rs=null;
		Record record = null;
		Connection conn = null;
    	try {
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	    	String sql = AchieveTrackSql.getArchieveInfoById(achieveid);
	    	rs=dbe.find(sql);
	    	
	    	if(rs!=null && rs.size()>0){
	    		record = rs.get(0);
	    	}
		} catch (SQLException e) {
			log.error("获取成果信息出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return record;
	}
	
	
	/**
	 * 获取成果跟踪信息，根据成果uuid，获取成果关联的跟踪信息
	 * **/
	public RecordSet achieveFollowInfoList(String achieveid){
		RecordSet rs=null;
		Record record = null;
		Connection conn = null;
    	try {
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	    	String sql = AchieveTrackSql.getFollowInfoByArchieveId(achieveid);
	    	rs=dbe.find(sql);
	    	
		} catch (SQLException e) {
			log.error("获取成果信息出错："+e);
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	/**
     * 删除技术需求信息
     * for ajax
     * @param 
     */
    public void deleteTrack(RequestKit requestKit)
    {
    	Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String trackid = requestKit.getParameter("trackid", "");
        	
        	//删除用户项目需求
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String uuid[] = trackid.split(",");
        	if(uuid!=null&&uuid.length>0){
        		for(int i=0;i<uuid.length;i++){
        			//逐条删除项目需求
        			String sql = AchieveTrackSql.deleteTrackById(uuid[i]);
        			m_executor.execute(sql);
        		}
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
}
